<template>
  <div>
    <!-- 头部 -->
    <div class="top">
      <van-image
        width="70"
        height="70"
        src="https://img01.yzcdn.cn/vant/cat.jpeg"
        round
      />
      <h3 v-if="ifLogined">{{username}}</h3>
      <h3 @click="ifshow = true" v-else>点击登录</h3>
      <van-icon
        :name="ifLogined ? 'cross' : 'arrow'"
        @click="loginout"
        size="16"
        color="white"
      />
    </div>
    <!-- 中间九宫格 -->
    <van-grid square :column-num="3">
      <van-grid-item
        v-for="item in gridArr"
        :key="item.id"
        :icon="item.icon"
        :text="item.type"
      />
    </van-grid>
    <!-- 模态框 -->
    <div class="motai" v-show="ifshow">
      <div class="motai-bg" @click="ifshow = false"></div>
      <div class="motai-content">
        <!-- 登录表单 -->
        <van-form @submit="onSubmit">
          <van-field v-model="username"
            name="用户名"
            label="用户名"
            placeholder="用户名"
            :rules="[{ required: true, message: '请填写用户名' }]"
          />
          <van-field
            v-model="password"
            type="password"
            name="密码"
            label="密码"
            placeholder="密码"
            :rules="[{ required: true, message: '请填写密码' }]"
          />
          <div style="margin: 16px">
            <van-button round block type="danger" native-type="submit"
              >提交</van-button
            >
          </div>
        </van-form>
      </div>
    </div>
    <!-- 底部导航组件 -->
    <tabbar></tabbar>
  </div>
</template>

<script>
// 引入登录方法
import { loginfn } from "../api/mineApi.js";
// 引入底部导航
import tabbar from "../components/Tabbar.vue";
export default {
  name: "",
  data() {
    return {
      username: "",//用户名
      password: "",//密码
      ifshow: false, //遮罩层显示
      ifLogined: false, // 登录状态
      gridArr: [
        // grid数组
        { id: 0, icon: "label-o", type: "我的订单" },
        { id: 1, icon: "bill-o", type: "优惠券" },
        { id: 2, icon: "goods-collect-o", type: "礼品卡" },
        { id: 3, icon: "location-o", type: "我的收藏" },
        { id: 4, icon: "flag-o", type: "我的足迹" },
        { id: 5, icon: "contact", type: "会员福利" },
        { id: 6, icon: "aim", type: "地址管理" },
        { id: 7, icon: "warn-o", type: "账号安全" },
        { id: 8, icon: "service-o", type: "联系客服" },
        { id: 9, icon: "question-o", type: "帮助中心" },
        { id: 10, icon: "smile-comment-o", type: "意见反馈" },
      ],
    };
  },
  components: {
    tabbar,
  },
   methods: {
    //  点击登录
    onSubmit(values) {
      // console.log(values);
      loginfn({ username: values["用户名"],pwd: values["密码"],}).then(res=>{
        if(res.errno == 0){
       this.$toast.success("登录成功");
        //将token传入本地存储
        localStorage.setItem('token',res.data.token)
        // 存储用户信息
        let str = JSON.stringify(res.data.userInfo);
        localStorage.setItem("userInfo", str);
        //  关闭遮罩层
        this.ifshow=false
        // 修改用户名
        this.username = res.data.userInfo.username;
        //改变登录状态
        this.ifLogined=true
        }
      })
    },
    // 点击退出登录
    loginout(){
      if(localStorage.getItem("token")){
           this.$dialog.confirm({
            title: "退出账号确认",
            message: "您是否想要退出当前账号？",
          }).then(()=>{
         // 清空数据
       localStorage.removeItem("token")
       localStorage.removeItem("userInfo")
         //  改变登录状态
       this.ifLogined=false   
      })
      }
      
    },
},
created(){
    // 是否登录
    if(localStorage.getItem("userInfo")){
      this.ifLogined=true
      let userInfo =JSON.parse(localStorage.getItem("userInfo"))
      this.username=userInfo.username
    }
  }
}
</script>
<style scoped lang='less'>
.top {
  width: 375px;
  height: 110px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #000;
  .van-image {
    margin-left: 10px;
  }
  h3 {
    width: 259px;
    height: 24px;
    color: white;
  }
  .van-icon {
    width: 16px;
    height: 16px;
  }
}
 .motai{
    width: 100%;
    height: 100%;
    position: fixed;
    z-index: 99;
    left: 0;
    top: 0;
    .motai-bg{
      width: 100%;
      height: 100%;
      background: rgba(0, 0, 0, 0.5);
    }
    .motai-content{
      width: 90%;
      height: 240px;
      position: absolute;
      background: #fff;
      left: 0;
      top: 0;
      right: 0;
      bottom: 0;
      margin: auto;
      padding-top: 60px;
    }
  }

</style>